package it.polimi.protopeer.simulation.manager;

public class MachineSSHAddress {
	private String username;
	private String host;
	private Integer port;
	private String password;
	private String pkey;
	private SSHConnection connection;
	private boolean connected;
	

	/**
	 * Costructor used with password
	 * @param username usermane
	 * @param host host name or ip address
	 * @param sshPort shh port
	 * @param numBrokers number of brokers
	 * @param password password
	 */
	public MachineSSHAddress(String username, String host, Integer sshPort, String password) {
		this.username = username;
		this.password=password;
		this.host = host;
		this.port = sshPort;
		this.connected=false;
	}
	
	/**
	 * Costructor used with private key
	 * @param username username
	 * @param host host name or ip address
	 * @param pkey private key
	 * @param numBrokers number of brokers
	 * @param sshPort ssh port
	 */
	public MachineSSHAddress(String username, String host, String pkey, Integer sshPort) {
		this.username = username;
		this.pkey=pkey;
		this.host = host;
		this.port = sshPort;
		this.connected=false;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getUsername() {
		return username;
	}
	
	public String getHost() {
		return host;
	}
	
	public Integer getPort() {
		return port;
	}

	public SSHConnection getConnection() {
		if(connected)
			return connection;
		else{
			if(pkey!=null && !pkey.equals(""))
				return connection=new SSHConnection(username, host, pkey, port);
			else
				return connection=new SSHConnection(username, host, port, password);
		}
	}

	public String sshAddress() {
		return "-p " + port + " " + username + "@" + host;
	}
	
	public String toString() {
		return sshAddress();
	}
	
	public int hashCode() {
		return sshAddress().hashCode();
	}
	
	public boolean equals(Object o) {
		return o.hashCode() == hashCode();
	}
}